﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Web;
using CMS.Core.DataInterfaces;
using CMS.Core.Domain;
using CMS.Data;
using AjaxPro;

namespace CMS.Presenters
{
    public class LookupConfigPresenter
    {
        ILookupConfigView view;
        XmlDocument xmlDoc = new XmlDocument();
        LookupConfigItem editObj;
        string strPathXML = "";
        /// <summary>
        /// Raised when the user wants to cancel editing a object.  The containing ASPX page should 
        /// listen for this event.
        /// </summary>
        public EventHandler CancelUpdateEvent;

        /// <summary>
        /// Raised after the object has been successfully committed to the database.
        /// </summary>
        public EventHandler UpdateCompleteEvent;

        /// <summary>
        /// Raised after the object has been successfully remove from the database.
        /// </summary>
        public EventHandler DeleteCompleteEvent;

        //Attaches this presenter to the view's events.
        public LookupConfigPresenter(ILookupConfigView view)
        {
            this.view = view;
            SubscribeViewToEvents();
        }
        public LookupConfigPresenter()
        {

        }

        public void InitViewForEditing(string objectId, bool isPostBack)
        {
            if (!isPostBack)
            {
                //this.editObj = this.dataService.GetById(Convert.ToByte(objectId));
            }
        }

        void SubscribeViewToEvents()
        {
            view.Load += OnViewLoad;
        }

        void OnViewLoad(object sender, EventArgs e)
        {
            if (!view.IsPostBack)
            {
                //LoadViewFromModel();
                //view.DataBind();
            }
        }

        public void LoadViewFromModel(String Action, String objId)
        {

            strPathXML = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/Config/LookupConfig.xml");
            try
            {
                xmlDoc.Load(strPathXML);
            }
            catch (Exception ex)
            {

            }
            IList ConfigList = new ArrayList();
            if (xmlDoc.DocumentElement != null)
            {
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes[0].ChildNodes)
                {
                    LookupConfigItem item = new LookupConfigItem();
                    item.Year = node.Attributes["key"].Value;
                    item.Database = node.Attributes["DB"]== null? "" : node.Attributes["DB"].Value;
                    item.UploadPath = node.Attributes["UploadPath"] == null ? "" : node.Attributes["UploadPath"].Value;
                    ConfigList.Add(item);
                    if (Action != null && Action == "EDIT" && item.Year == objId)
                    {
                        view.Year = item.Year;
                        view.Database = item.Database;
                        view.UploadPath = item.UploadPath;
                        this.editObj = item;
                    }
                }
            }

            view.ConfigList = ConfigList;
        }

        LookupConfigItem ObjectToUpdate
        {
            get
            {
                LookupConfigItem gObj = this.editObj;
                if (gObj == null)
                {
                    gObj = new LookupConfigItem();
                }
                gObj.Year = view.Year;
                gObj.Database = view.Database;
                gObj.UploadPath = view.UploadPath;

                return gObj;
            }
            set
            {
                if (value == null) throw new ArgumentNullException("ObjectToUpdate may not be null");
            }
        }

        public void CancelUpdate()
        {
            CancelUpdateEvent(this, null);
        }

        public void UpdateObject()
        {
            LookupConfigItem item = this.ObjectToUpdate;
            if (this.editObj == null)
            {
                XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, "item", null);
                XmlAttribute xa = xmlDoc.CreateAttribute("key");
                xa.Value = item.Year;
                node.Attributes.Append(xa);
                xa = xmlDoc.CreateAttribute("DB");
                xa.Value = item.Database;
                node.Attributes.Append(xa);
                xa = xmlDoc.CreateAttribute("UploadPath");
                xa.Value = item.UploadPath;
                node.Attributes.Append(xa);
                xmlDoc.DocumentElement.ChildNodes[0].AppendChild(node);
            }
            else
            {
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes[0].ChildNodes)
                {
                    if (node.Attributes["key"].Value == item.Year)
                    {
                        node.Attributes["DB"].Value = item.Database;
                        node.Attributes["UploadPath"].Value = item.UploadPath;
                    }
                }
            }
            try
            {
                xmlDoc.Save(strPathXML);
            }
            catch (Exception ex)
            {

            }
            UpdateCompleteEvent(this, null);
        }

        public void DeleteObject(String Id)
        {
            String[] Ids = Id.Split(',');
            foreach(String year in Ids)
            {
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes[0].ChildNodes)
                {
                    if (node.Attributes["key"].Value == year)
                    {
                        xmlDoc.DocumentElement.ChildNodes[0].RemoveChild(node);
                        break;
                    }
                }
            }
            try
            {
                xmlDoc.Save(strPathXML);
            }
            catch (Exception ex)
            {

            }
            DeleteCompleteEvent(this, null);
        }
    }

    public class LookupConfigItem
    {
        String year;
        String database;
        String uploadPath;
        public LookupConfigItem()
        { 
        }
        public String Year {
            get {return this.year;}
            set { this.year = value; }
        }

        public String Database
        {
            get { return this.database; }
            set { this.database = value; }
        }

        public String UploadPath
        {
            get { return this.uploadPath; }
            set { this.uploadPath = value; }
        }

    }
}
